package com.luo.contentprovider.ui;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import com.luo.contentprovider.domain.PersonObserver;
import com.luo.contentprovider.domain.db.PersonDao;
import com.luo.contentprovider.domain.db.PersonDaoImpl;
import com.luo.contentprovider.domain.db.PersonSQLTable;

/**
 * 
 * @author lenovo
 *
 */
public class PersonActivity extends Activity {

    private Handler hand;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        
        View add = findViewById(R.id.addPerson);
        View query = findViewById(R.id.getPerson);
        
        query.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				  Cursor cc  = PersonDaoImpl.getInstance(PersonActivity.this).getAllPerson();
				  if(cc != null && cc.moveToFirst()){
					  
					  Log.i("prt", "ffff---"+cc.getInt(cc.getColumnIndex(PersonSQLTable.COLUMN_ID)));
					  
					  while (cc.moveToNext())
					{
						  Log.i("prt", "---"+cc.getInt(cc.getColumnIndex(PersonSQLTable.COLUMN_ID)));
						  
					}
				  }
				  
			}
		});
        
        
        
        
        hand = new Handler();
        add.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v)
			{
				
				Uri.Builder bu = new Uri.Builder();
//				bu.scheme(MyProvider.SECHEM).authority(MyProvider.URL_STR).appendPath("person");//错
				bu.scheme(MyProvider.SECHEM_NAME).authority(MyProvider.AUTHORITY).appendPath("person");
				Uri myuri =  bu.build();;
				
				ContentValues values = new ContentValues();
				values.put(PersonSQLTable.COLUMN_NAME, "luo");
				values.put(PersonSQLTable.COLUMN_AGE, 12);
				values.put(PersonSQLTable.COLUMN_NUM, "1221211231365644111");
				
				
				//监听
				getContentResolver().registerContentObserver(myuri,
						true, new PersonObserver(hand));

				
				getContentResolver().insert(myuri , values);
				
			}
        	
        });
        
    }

}
